#!/usr/bin/python3
# -*- coding:utf-8 -*-
# @Author    : jltxseo
# @File      : UrllibTestPy3.py
# @Time      : 2017/12/4 0:00
# @Software  : PyCharm


import urllib.request
import urllib.parse
import os
import sys

"""
获取固定网页的内容
"""


def get_baidu_content():
    url = 'http://www.jianshu.com/trending/weekly?page={}'
    headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/57.0.2987.133 '
                             'Safari/537.36'}
    request = urllib.request.Request(url=url, headers=headers)
    html = urllib.request.urlopen(request)
    # html = urllib.request.urlopen(url, None, 1000)
    print(html.read())


"""
urllib库进行get请求的示例
"""


def url_get(url):
    data = urllib.parse.urlencode({'param1': 'hello', 'param2': 'wrold'})
    print(url)
    print(data)
    new_url = '?'.join([url, "%s"]) % data
    print(new_url)
    res = urllib.request.urlopen(new_url)
    print('>>>>>>Res content>>>>>')
    print(res.read())
    print('>>>>>>> Res content End')


"""
    获取当前路径
"""


def os_path():
    print("__file__=%s" % __file__)

    print("os.path.realpath(__file__)=%s" % os.path.realpath(__file__))

    print("os.path.dirname(os.path.realpath(__file__))=%s" % os.path.dirname(os.path.realpath(__file__)))

    print("os.path.split(os.path.realpath(__file__))=%s" % os.path.split(os.path.realpath(__file__))[0])

    print("os.path.abspath(__file__)=%s" % os.path.abspath(__file__))

    print("os.getcwd()=%s" % os.getcwd())

    print("sys.path[0]=%s" % sys.path[0])

    print("sys.argv[0]=%s" % sys.argv[0])


if __name__ == '__main__':
    # get_baidu_content()
    # url_get1 = "http://httpbin.org/get"
    # url_get(url_get1)
    # os_path()


